package com.example.myapp.ADO;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapp.Activity.Friendshome;
import com.example.myapp.Activity.VolleyChat;
import com.example.myapp.Loading.ShapeLoadingDialog;
import com.example.myapp.Model.Msg;
import com.example.myapp.Model.User;
import com.example.myapp.R;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.CountListener;
import de.hdodenhof.circleimageview.CircleImageView;

public class FriendAapter extends BaseAdapter {

    private Context context;
    private List<User> userList;
    private LayoutInflater mInflater;
    private ShapeLoadingDialog shapeLoadingDialog;
    private String isUserPagecome;

    //自定义adaper开始
    public FriendAapter(Context context, List<User> userList,String isUserPagecome) {
        this.context = context;
        this.userList = userList;
        this.isUserPagecome = isUserPagecome;
        mInflater = LayoutInflater.from(context);//初始化

    }



    @Override
    public boolean areAllItemsEnabled() {
        return super.areAllItemsEnabled();
    }

    @Override
    public int getCount() {
        return userList.size();
    }

    @Override
    public Object getItem(int i) {
        return userList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = mInflater.inflate(R.layout.myfrienditem, null);
        CircleImageView myfrend_icon = view.findViewById(R.id.myfrend_icon);
        TextView myfrendusernmae = view.findViewById(R.id.myfrendusernmae);
        TextView myfrendjianjie = view.findViewById(R.id.myfrendjianjie);
        TextView tt_yincang = view.findViewById(R.id.tt_yincang);
        TextView myfrendmsgcount = view.findViewById(R.id.myfrendmsgcount);
        LinearLayout myfrendlayoutbtn= view.findViewById(R.id.myfrendlayoutbtn);
        ImageView myfrendbtn= view.findViewById(R.id.myfrendbtn);

        String str= userList.get(i).getShow();
        if (str.length()>14){
            tt_yincang.setVisibility(View.VISIBLE);//mlistview2处于显示状态
            myfrendjianjie.setText("简介："+str.substring(0,11));
        }else {
            myfrendjianjie.setText("简介："+str);
        }

        Glide.with(context).load(userList.get(i).getIcon().getUrl()).into(myfrend_icon);// CircleImageView.显示网络图片特殊方法
        myfrendusernmae.setText(userList.get(i).getUsername());

        myfrendlayoutbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, Friendshome.class);
                intent.putExtra("userID", userList.get(i).getObjectId());
                intent.putExtra("iconUrl",userList.get(i).getIcon().getUrl());
                intent.putExtra("userName",userList.get(i).getUsername());
                if (userList.get(i).getShow()!=null){
                    intent.putExtra("jianJie",userList.get(i).getShow());
                }
                context.startActivity(intent);
            }
        });//详情页面
        myfrendbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Intent intent = new Intent(context, VolleyChat.class);
                intent.putExtra("userID", userList.get(i).getObjectId());
                intent.putExtra("iconUrl",userList.get(i).getIcon().getUrl());
                intent.putExtra("userName",userList.get(i).getUsername());
                intent.putExtra("isUserPagecome",isUserPagecome);

                if (userList.get(i).getShow()!=null){
                    intent.putExtra("jianJie",userList.get(i).getShow());
                }
                context.startActivity(intent);
            }
        });//聊天页面
        BmobQuery<Msg> query1 = new BmobQuery<Msg>();
        query1.addWhereEqualTo("author",userList.get(i).getObjectId());
        BmobQuery<Msg> query2 = new BmobQuery<Msg>();
        query2.addWhereEqualTo("other", BmobUser.getCurrentUser(User.class).getObjectId());
        List<BmobQuery<Msg>> queries = new ArrayList<BmobQuery<Msg>>();
        queries.add(query1);
        queries.add(query2);
        BmobQuery<Msg> query=new BmobQuery<>();
        query.and(queries);
        query.count(Msg.class, new CountListener() {
            @Override
            public void done(Integer count, BmobException e) {
                if(e==null){
                    Log.w("aaaa","收到消息条数："+count);
                    final    SQLDAO sqldao=new SQLDAO(context);
                   if( sqldao.getMsgcount(userList.get(i).getObjectId())<count) {
                       myfrendmsgcount.setVisibility(View.VISIBLE);
                       int a = count - sqldao.getMsgcount(userList.get(i).getObjectId());
                       myfrendmsgcount.setText("" + a);
                   }
                }else{
                    Log.w("aaaa","失败："+e.getMessage()+","+e.getErrorCode());
                }
            }
        });









        return view;
    }






    //网络加载
    private  void openLoading(){
        shapeLoadingDialog = new ShapeLoadingDialog.Builder(context)
                .loadText("加载中...")
                .build();
        shapeLoadingDialog.show();
    }
    //关闭网络加载
    private  void closeLoading(){
        shapeLoadingDialog.dismiss();
    }

}
